package org.deadline.model.config;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.deadline.model.user.Person;
import org.deadline.repository.config.StateProvinceRepository;
import org.deadline.service.config.StateProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Configurable
@Component
public class StateProvinceDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<StateProvince> data;

	@Autowired
    private CountryDataOnDemand countryDataOnDemand;

	@Autowired
    StateProvinceService stateProvinceService;

	@Autowired
    StateProvinceRepository stateProvinceRepository;

	public StateProvince getNewTransientStateProvince(int index) {
        StateProvince obj = new StateProvince();
        setCreatedBy(obj, index);
        setCreationDate(obj, index);
        setDeleted(obj, index);
        setModificationDate(obj, index);
        setStateCapital(obj, index);
        setStateCode(obj, index);
        setStateName(obj, index);
        return obj;
    }

	public void setCreatedBy(StateProvince obj, int index) {
        Person createdBy = null;
        obj.setCreatedBy(createdBy);
    }

	public void setCreationDate(StateProvince obj, int index) {
        Date creationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setCreationDate(creationDate);
    }

	public void setDeleted(StateProvince obj, int index) {
        Boolean deleted = true;
        obj.setDeleted(deleted);
    }

	public void setModificationDate(StateProvince obj, int index) {
        Date modificationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setModificationDate(modificationDate);
    }

	public void setStateCapital(StateProvince obj, int index) {
        String stateCapital = "stateCapital_" + index;
        if (stateCapital.length() > 100) {
            stateCapital = stateCapital.substring(0, 100);
        }
        obj.setStateCapital(stateCapital);
    }

	public void setStateCode(StateProvince obj, int index) {
        String stateCode = "stateCode_" + index;
        if (stateCode.length() > 100) {
            stateCode = stateCode.substring(0, 100);
        }
        obj.setStateCode(stateCode);
    }

	public void setStateName(StateProvince obj, int index) {
        String stateName = "stateName_" + index;
        if (stateName.length() > 100) {
            stateName = stateName.substring(0, 100);
        }
        obj.setStateName(stateName);
    }

	public StateProvince getSpecificStateProvince(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        StateProvince obj = data.get(index);
        Long id = obj.getId();
        return stateProvinceService.findStateProvince(id);
    }

	public StateProvince getRandomStateProvince() {
        init();
        StateProvince obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return stateProvinceService.findStateProvince(id);
    }

	public boolean modifyStateProvince(StateProvince obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = stateProvinceService.findStateProvinceEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'StateProvince' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<StateProvince>();
        for (int i = 0; i < 10; i++) {
            StateProvince obj = getNewTransientStateProvince(i);
            try {
                stateProvinceService.saveStateProvince(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            stateProvinceRepository.flush();
            data.add(obj);
        }
    }
}
